package com.alit.xiaoitts;

import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketFactory;

import java.io.IOException;

public class XiaoiTTSConnector {
	private WebSocket clinet1;

	public void connect(String uri) throws IOException, WebSocketException {
		this.clinet1 = new WebSocketFactory().createSocket(uri).addListener(new WebSocketAdapter() {
			@Override
			public void onTextMessage(WebSocket websocket, String message) {
				System.out.println("in_json = " + message);
			}
		}).connect();
	}

	public void addListener(WebSocketAdapter webSocketAdapter) {
		clinet1.addListener(webSocketAdapter);
	}

	public void sendBinary(byte[] pcms) throws IOException {
		clinet1.sendBinary(pcms);
	}

	public void sendText(String str) throws IOException {
		clinet1.sendText(str);
	}

	public void disconnect() throws InterruptedException {
		clinet1.sendClose();
		clinet1.disconnect();
	}

}
